package com.woniuxy.carrental.controller;


import com.woniuxy.carrental.entity.Carpart;
import com.woniuxy.carrental.entity.PageBean;
import com.woniuxy.carrental.entity.ResponseResult;
import com.woniuxy.carrental.service.CarPartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author xilei
 * @create 2021-10-03 上午 11:20
 */
@RestController
public class CarPartController {

    @Autowired
    CarPartService carPartService;

    /**
     * 分页查找
     * @param pageIndex
     * @param pageSize
     * @param carpart
     * @return
     */
    @GetMapping("/carpartlist")
    public ResponseResult<PageBean<Carpart>> getAllCar(Integer pageIndex, Integer pageSize, Carpart carpart){
        List<Carpart> carpartBypage = carPartService.findCarpartBypage(pageIndex, pageSize);
        for(Carpart a:carpartBypage){
            System.out.println(a);
        }
        PageBean<Carpart> pageBean = new PageBean<>();
        pageBean.setTotalRecord(carPartService.findCarpart().size());
        pageBean.setPageSize(pageSize);
        pageBean.setPageIndex(pageIndex);
        pageBean.setBeanList(carpartBypage);
        return new ResponseResult<>(pageBean, 200, "OK");
    }

    /**
     * 模糊查询
     * @param carpart_name
     * @param carpart_type
     * @param carpart_vehicle
     * @param pageIndex
     * @param pageSize
     * @param carpart
     * @return
     */
    @GetMapping("/carpartlist1")
    public ResponseResult<PageBean<Carpart>> getAllBytiaojian(String carpart_name, String carpart_type, String carpart_vehicle, Integer pageIndex, Integer pageSize,Carpart carpart){
        List<Carpart> carpartBypageByparm = carPartService.findCarpartBypageByparm(carpart_name, carpart_type, carpart_vehicle, pageIndex, pageSize);
        for(Carpart a:carpartBypageByparm){
            System.out.println(a);
        }
        PageBean<Carpart> pageBean = new PageBean<>();
        pageBean.setBeanList(carpartBypageByparm);
        pageBean.setPageSize(pageSize);
        pageBean.setPageIndex(pageIndex);
        pageBean.setTotalRecord(carPartService.findCarpart().size());
        return new ResponseResult<>(pageBean,200,"OK");
    }

    /**
     * 增汽配信息
     * @param carpart
     * @return
     */
    @PostMapping("/carpartlist")
    public ResponseResult addCarpart(@RequestBody Carpart carpart){
        System.out.println("新增的汽配为："+carpart);
        carPartService.insertCarpart(carpart);
        return new ResponseResult(null, 200, "新增汽配成功");
    }

    /**
     * 删汽配信息
     * @param carpart_id
     * @return
     */
    @DeleteMapping("/carpartlist")
    public ResponseResult deleteCarpart(Integer carpart_id){
        System.out.println("删除的汽配编号为："+carpart_id);
        carPartService.deleteCarpart(carpart_id);
        return new ResponseResult(null, 200, "删除汽配成功");
    }

    /**
     * 改汽配信息
     * @param carpart
     * @return
     */
    @PutMapping("/carpartlist")
    public ResponseResult updateCarpart(@RequestBody Carpart carpart){
        System.out.println("修改后的汽配为："+carpart);
        carPartService.updateCarpart(carpart.getCarpart_name(), carpart.getCarpart_type(), carpart.getCarpart_vehicle(), carpart.getCarpart_picture(), carpart.getCarpart_id());
        return new ResponseResult(null, 200, "修改汽配成功");
    }

}
